package game;

import behaviors.Position;
import behaviors.Rotation;
import engine.hw.Gamepad;
import engine.hw.RunTimer;
import math.vector.Vecmath;
import math.vector.vec3f;

public class ViewMotionGamepad extends ViewMotion
{
    
    private static final float MOTION_SENS_X = 1.0f;
    private static final float MOTION_SENS_Y = 1.0f;
    
    private vec3f dirLeftStick;
    
    public ViewMotionGamepad(
            Position viewPosition, 
            Rotation viewRotation) 
    {
        super(
            viewPosition, 
            viewRotation
        );
        this.dirLeftStick = new vec3f();
    }
    
    public void update() 
    {
        float time = RunTimer.get().getFrameTime();
        
        float dX = (
            ViewMotionGamepad.MOTION_SENS_X * 
            Gamepad.get().getLeftStickXValue()
        );
        float dY = (
            ViewMotionGamepad.MOTION_SENS_Y * 
            Gamepad.get().getLeftStickYValue()
        );
        this.dirLeftStick.x = dX;
        this.dirLeftStick.z = dY;
        
        vec3f rotatedDir = Vecmath.getRotationOf( // Rotate gamepad inputs
            this.dirLeftStick, // Source vector
            Vecmath.AXIS_Y, // Axis vector
            -this.viewRotation.getRots().y // AngleRad to rotate
        );
        
//        this.motionVelocity.translate(rotatedDir); // Add on gamepad inputs
//        
//        Vecmath.calcScaledCopyOf( // Determine the motion distance vector this frame
//            this.motionVelocity.x, this.motionVelocity.y, this.motionVelocity.z, // Source
//            time, time, time, // Scale factors
//            this.motionDistance // Destination vector
//        );
        
        this.motionDistance.setToZero();
        this.motionDistance.translate(rotatedDir);
        this.motionDistance.scale(time);
        
       this.viewPosition.translateAdd(motionDistance);
    }
    
    public void destroy()
    {
        super.destroy();
        this.dirLeftStick = null;
    }
    
}
